package com.daocao.hua.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daocao.hua.R;
import com.daocao.hua.api.callback.ContactUsContract;
import com.daocao.hua.api.requestInfo.ContactUsRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.ContactUsModel;
import com.daocao.hua.model.ContentDetailModel;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.view.CircularProgress;
import com.daocao.hua.view.Dialog;

/**
 * 联系我们界面
 * Created by Master.Dong on 16/4/10.
 */
public class ContactUsActivity extends PermissionActivity implements View.OnClickListener, ContactUsContract.View {

    private TextView sPhone, email, sTime;
    private String phone;
    private CircularProgress loading;
    private ContactUsRequest api;
    private TextView txtIntroduce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.contact_us_activity);
        baseInitView(this, "ContactUsActivity", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        api = ContactUsRequest.create();
        api.attachView(this);
        setTitleText("关于我们");
        setBackIntent();

        loading = (CircularProgress) findViewById(R.id.progressBar);
        sPhone = (TextView) findViewById(R.id.service_phone_tv);
        email = (TextView) findViewById(R.id.email_tv);
        sTime = (TextView) findViewById(R.id.service_time_tv);
        txtIntroduce = (TextView) findViewById(R.id.txt_introduce);

        sPhone.setText(Const.SERVICE_PHONE);
        email.setText(Const.SERVICE_EMAIL);
        sTime.setText(Const.SERVICE_TIME);

        ((RelativeLayout) findViewById(R.id.service_phone)).setOnClickListener(this);
        ((RelativeLayout) findViewById(R.id.email)).setOnClickListener(this);


        getContact();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.service_phone:
                phone = sPhone.getText().toString().trim();
                call(phone);
                break;
            case R.id.email:
                try {
                    Intent data = new Intent(Intent.ACTION_SENDTO);
                    data.setData(Uri.parse("mailto:" + email.getText().toString()));
                    if (data.resolveActivity(getPackageManager()) != null) {
                        startActivity(data);
                    } else {
                        ToastUtil.makeText(ContactUsActivity.this, getResources().getString(R.string.no_email_app_text), Toast.LENGTH_SHORT).show();
                    }
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    /**
     * 拨打电话
     */
    private void call(final String phone) {


        Dialog.showDefaultDialog(this, getResources().getString(R.string.tip), getResources().getString(R.string.call_dob_phone) + phone, getResources().getString(R.string.cancel), getResources().getString(R.string.dial), true, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            @Override
            public void cancel() {

            }
        });
    }


    /**
     * 获取内容
     */
    private void getContact() {
        api.getContactUs();
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (loading != null) {
            if (b)
                loading.setVisibility(View.VISIBLE);
            else
                loading.setVisibility(View.GONE);
        }
    }


    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getContactUsSuccess(ContactUsModel model) {
        sPhone.setText(model.getService_phone());
        email.setText(model.getEmail());
        sTime.setText(model.getService_time());
        if (model.content != null && model.content.size() != 0) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < model.content.size(); i++) {
                builder.append("\u3000\u3000" + model.content.get(i) + "\n");

            }
            txtIntroduce.setText(builder);
        }
    }

    @Override
    public void getAboutSuccess(ContentDetailModel item) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;

    }
}
